package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.hxkcloud.patient.R;
import com.hxkcloud.patient.beans.HospitalStatusBean;
import com.hxkcloud.patient.beans.UserManagerBean;
import com.hxkcloud.patient.dborm.HospitalStatusDao;
import com.hxkcloud.patient.utils.ImageOptionUtils;
import com.hxkcloud.patient.utils.StringUtil;
import com.hxkcloud.patient.views.CircleImageView;

import java.util.Calendar;
import java.util.List;

/**
 * Created by ray_lei on 2016/7/6.
 */
public class UserManagerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context mContext;
    private List<UserManagerBean> datas;
    private RecyclerView recyclerview;
    private LayoutInflater mInflater;

    private ResultSelectedListener listener;

    List<HospitalStatusBean> beans;
    protected ImageLoader imageLoader;
    private int screenWidth;

    public UserManagerAdapter(Context mContext, List<UserManagerBean> datas,
                              RecyclerView recyclerview, ResultSelectedListener listener, int width, String departmentId) {
        this.mContext = mContext;
        this.datas = datas;
        this.recyclerview = recyclerview;
        this.listener = listener;
        this.screenWidth = width;
        mInflater = LayoutInflater.from(mContext);
        imageLoader = ImageLoader.getInstance();
        HospitalStatusDao dao = new HospitalStatusDao(mContext);
        beans = dao.getHospitalStatus(StringUtil.isCurChanke(departmentId, mContext));
    }

    public void setData(List<UserManagerBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void clearData() {
        datas.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        // TODO Auto-generated method stub
        return datas.size();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // TODO Auto-generated method stub


        Holder mHolder = (Holder) holder;
        mHolder.nameTV.setText(datas.get(position).getName());
        LinearLayout.LayoutParams lp_head = new LinearLayout.LayoutParams(screenWidth / 6, screenWidth / 6);
        lp_head.setMargins(10, 0, 0, 0);
        mHolder.iconCI.setLayoutParams(lp_head);

        String ageString = "";
        if (TextUtils.isEmpty(datas.get(position).getAge())) {
            ageString = "";
        } else {
            Calendar calendar = Calendar.getInstance();
            int curYear = calendar.get(Calendar.YEAR);
            int ageYear = Integer.valueOf(datas.get(position).getAge());
            int birthYear = curYear - ageYear;
            ageString = "" + birthYear;
        }
        imageLoader.displayImage(datas.get(position).getHeadImage(), mHolder.iconCI, ImageOptionUtils.getInstance().getPatientHeadOptions(ageString, datas.get(position).getSex()));
        if (datas.get(position).getAge().equals("0")) {
            mHolder.ageTV.setText("<1" + mContext.getResources().getString(R.string.usermanger_text_age));
        } else {
            mHolder.ageTV.setText(datas.get(position).getAge() + mContext.getResources().getString(R.string.usermanger_text_age));
        }
        if ("0".equals(datas.get(position).getSex())) {
            mHolder.sexTV.setText(mContext.getResources().getString(R.string.male));
        } else if ("1".equals(datas.get(position).getSex())) {
            mHolder.sexTV.setText(mContext.getResources().getString(R.string.female));
        }
        if (beans != null) {
            String status = datas.get(position).getHospitalStatus();
            for (int i = 0; i < beans.size(); i++) {
                if (status.equals(beans.get(i).getItemId() + "")) {
                    mHolder.statusTV.setText(beans.get(i).getName());
                    break;
                }

            }
        }
//        if(datas.get(position).getDiseaseList() != null){
//            StringBuffer stringBuffer = new StringBuffer();
//            int size = datas.get(position).getDiseaseList().size();
//            for(int i = 0; i < size; i++){
//                DiseaseBean bean = datas.get(position).getDiseaseList().get(i);
//                stringBuffer.append(bean.getName());
//                if(i != (size - 1)){
//                    stringBuffer.append("\u007C");
//                }
//
//
//            }
//            mHolder.descrptionTV.setText(stringBuffer.toString());
//        }

        mHolder.descrptionTV.setText(datas.get(position).getTags());
        if (datas.get(position).getMonitorStatus() == 0) {
            mHolder.redPointIV.setVisibility(View.INVISIBLE);
        } else {
            mHolder.redPointIV.setVisibility(View.VISIBLE);
        }

        mHolder.monitorTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onSelected("monitor", datas.get(position));
            }
        });
        mHolder.remindTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onSelected("remind", datas.get(position));
            }
        });
        mHolder.chatTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onSelected("chat", datas.get(position));
            }
        });
        mHolder.layout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                listener.onSelected("item", datas.get(position));

            }

        });

        String selfCare = datas.get(position).getSelfCare();
        if (TextUtils.isEmpty(selfCare)) {
            mHolder.levelTV.setVisibility(View.INVISIBLE);
        } else if (selfCare.equals(mContext.getResources().getString(R.string.selfcare_level1))) {
            mHolder.levelTV.setVisibility(View.VISIBLE);
            mHolder.levelTV.setText(mContext.getResources().getString(R.string.degaree_text_level1));
            mHolder.levelTV.setBackgroundResource(R.drawable.bg_text_line_level1);
            mHolder.levelTV.setTextColor(mContext.getResources().getColor(R.color.color_level1));
        } else if (selfCare.equals(mContext.getResources().getString(R.string.selfcare_level2))) {
            mHolder.levelTV.setVisibility(View.VISIBLE);
            mHolder.levelTV.setText(mContext.getResources().getString(R.string.degaree_text_level2));
            mHolder.levelTV.setBackgroundResource(R.drawable.bg_text_line_level2);
            mHolder.levelTV.setTextColor(mContext.getResources().getColor(R.color.color_level2));
        } else if (selfCare.equals(mContext.getResources().getString(R.string.selfcare_level3))) {
            mHolder.levelTV.setVisibility(View.VISIBLE);
            mHolder.levelTV.setText(mContext.getResources().getString(R.string.degaree_text_level3));
            mHolder.levelTV.setBackgroundResource(R.drawable.bg_text_line_level3);
            mHolder.levelTV.setTextColor(mContext.getResources().getColor(R.color.color_level3));
        } else if (selfCare.equals(mContext.getResources().getString(R.string.selfcare_level4))) {
            mHolder.levelTV.setVisibility(View.VISIBLE);
            mHolder.levelTV.setText(mContext.getResources().getString(R.string.degaree_text_level4));
            mHolder.levelTV.setBackgroundResource(R.drawable.bg_text_line_level4);
            mHolder.levelTV.setTextColor(mContext.getResources().getColor(R.color.color_level4));
        }

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        View v = mInflater.inflate(R.layout.item_usermanager_layout, null);
        ViewHolder holer = new Holder(v);

        return holer;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    /*
     * Holder
     */
    public class Holder extends ViewHolder {

        TextView nameTV;
        TextView ageTV;
        TextView sexTV;
        TextView statusTV;
        CircleImageView iconCI;
        TextView descrptionTV;

        TextView monitorTV;
        TextView remindTV;
        TextView chatTV;

        LinearLayout layout;

        ImageView redPointIV;

        TextView levelTV;

        public Holder(View view) {
            super(view);
            nameTV = (TextView) view.findViewById(R.id.usermanager_tv_name);
            ageTV = (TextView) view.findViewById(R.id.usermanager_tv_age);
            sexTV = (TextView) view.findViewById(R.id.usermanager_tv_sex);
            statusTV = (TextView) view.findViewById(R.id.usermanager_tv_status);
            iconCI = (CircleImageView) view.findViewById(R.id.usermanager_ci_icon);
            descrptionTV = (TextView) view.findViewById(R.id.usermanager_tv_desciption);
            monitorTV = (TextView) view.findViewById(R.id.usermanager_tv_monitor);
            remindTV = (TextView) view.findViewById(R.id.usermanager_tv_remind);
            chatTV = (TextView) view.findViewById(R.id.usermanager_tv_chat);
            layout = (LinearLayout) view.findViewById(R.id.usermanager_layout_itemwhole);

            redPointIV = (ImageView) view.findViewById(R.id.usermanager_iv_redpoint);
            levelTV = (TextView) view.findViewById(R.id.usermanager_tv_level);

        }
    }

    public interface ResultSelectedListener {
        void onSelected(String type, UserManagerBean info);
    }


}
